import React from 'react';
import { Empty } from 'antd';
import { getDimissionTalkDetail } from '@/pages/OnboardAndLeave/LeaveManagement/service';
import { useRequest, defaultFormatResult } from '@/hooks';

import TalkStart from './TalkStart';
import TalkProcess from './TalkProcess';
import TalkResult from './TalkResult';
import TalkDrawer from './TalkDrawer';
import { TALK_STATUS_ENUM } from '../../constants';

export const formatList = (originData = []) =>
    originData.map(item => ({
        ...item,
        interviewStatus: Number(item.interviewStatus),
        interviewWay: Number(item.interviewWay),
        interviewEmployeeCodeList: (item.interviewerEmployeeInfo ?? []).map(it => it.employeeCode),
        interviewArrangeCode: item.id
    }));

export const getApplyData = (list = []) => {
    if (!list.length) return {};
    const lastData = list[list.length - 1];
    return lastData.interviewStatus === TALK_STATUS_ENUM.DONE ? {} : lastData; // 最后一条为完成时
};

const Talk = ({ processInstanceId, employeeCode, employeeName, isEdit }) => {
    const [visible, setVisible] = React.useState(false);

    const { data: originData = [], run: refresh } = useRequest(() => getDimissionTalkDetail(processInstanceId), {
        ready: !!processInstanceId,
        ...defaultFormatResult
    });

    const list = formatList(originData);
    const applyData = getApplyData(list);

    const drawerProps = {
        visible,
        setVisible,
        initialData: applyData, // 设计的限制只能编辑/删除最新
        employeeCode,
        employeeName,
        processInstanceId,
        callback: refresh
    };

    // 这样的设计，again and again，审批人（isEdit）能创建，修改，删除面谈

    // 审批人
    const workFlowUser = isEdit;
    const showStartButton = workFlowUser && !applyData.id;

    const renderContent = React.useCallback(
        (data, index) => {
            const { interviewStatus } = data;
            const title = `面谈${index + 1}`;
            let content;
            switch (interviewStatus) {
                case TALK_STATUS_ENUM.PREPARE:
                case TALK_STATUS_ENUM.DOING:
                case TALK_STATUS_ENUM.CONCLUSION:
                case TALK_STATUS_ENUM.CANCEL:
                    content = <TalkProcess data={data} isEdit={isEdit} setVisible={setVisible} title={title}/>;
                    break;
                case TALK_STATUS_ENUM.DONE:
                    content = <TalkResult data={data} title={title}/>;
                    break;
                default:
                    content = null;
            }
            return content;
        },
        [setVisible, isEdit]
    );

    return (
        <div style={{ marginBottom: 16 }}>
            {!isEdit && !list.length && <Empty />}
            {list.map((item, index) => (
                <div key={item.id} style={{ marginBottom: 16, width: 725 }}>
                    {renderContent(item, index)}
                </div>
            ))}
            {showStartButton && (
                <div style={{ width: 725 }}>
                    <TalkStart
                        onClick={() => {
                            setVisible(true);
                        }}
                    />
                </div>
            )}
            <TalkDrawer {...drawerProps} />
        </div>
    );
};

export default Talk;
